from tkinter import messagebox


class User:

    def __init__(self, user_data):
        self.id = user_data[0]
        self.username = user_data[1]
        self.password_hash = user_data[2]
        self.role = user_data[3]
        self.email = user_data[4] if len(user_data) > 4 else None
        self.phone = user_data[5] if len(user_data) > 5 else None
        self.created_at = user_data[6] if len(user_data) > 6 else None

    def verify_password(self, password):
        try:
            from werkzeug.security import check_password_hash
            return check_password_hash(self.password_hash, password)
        except Exception as e:
            messagebox.showerror("密码验证错误", f"密码验证失败: {str(e)}")
            return False

    @classmethod
    def create_user(cls, username, password_hash, role, email=None, phone=None):
        """创建用户对象的工厂方法"""
        user_data = (None, username, password_hash, role, email, phone, None)
        return cls(user_data)

